//
//  NewsContentViewController.m
//  xpkc
//
//  Created by TopSage iOS on 15/2/27.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "NewsContentViewController.h"
#import "GetTime.h"
#import <WebKit/WebKit.h>
@interface NewsContentViewController (){
    UIWebView *webView;
    //    UIButton *webBack;
}

@end

@implementation NewsContentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //        self.navigationItem.title = @"备考资讯";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatNewsContnetView];
}
- (void)creatNewsContnetView{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100)];
    label.text = @"来自学派网 www.studypay.com";
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height-50)];
    webView.backgroundColor = CCOLOR;
    webView.delegate = self;
    [self.view addSubview:webView];
    
    //    webBack = [UIButton buttonWithType:UIButtonTypeCustom];
    //    webBack.frame = CGRectMake(SCREEN_WIDTH - 80, SCREEN_HEIGHT - 64 - 40 - 80, 80, 80);
    //    [webBack setBackgroundColor:[UIColor redColor]];
    //    [webBack setTitle:@"<-" forState:UIControlStateNormal];
    //    [webBack addTarget:self action:@selector(webgoback) forControlEvents:UIControlEventTouchUpInside];
    //    [self.view addSubview:webBack];//再说
    
    [self requestNewsContent];
}

- (void)requestNewsContent{
    
    NSString *module = @"news";
    NSString *interface = @"newsdetail";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"newsid":self.newsid};
    logdebug(@"---%@",self.newsid);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        //        logdebug(@"%@",dic);
        NSDictionary *bodydic = [dic valueForKey:@"info"];
        NSString *body = [bodydic valueForKey:@"body"];
        
        NSMutableString * mutStr = [NSMutableString stringWithString:body];
        
        if ([body containsString:@"<img src="]) {
            NSRange range = [mutStr rangeOfString:@"<img src="];
            
            NSRange range2 = [mutStr rangeOfString:@".png\"/>" options:NSCaseInsensitiveSearch range:NSMakeRange(range.location,body.length - range.location - 1)];
            
            NSRange range3 = NSMakeRange(range.location, range2.location + range2.length - range.location);
            
            
            [mutStr replaceCharactersInRange:range3 withString:@""];
        }
        
        
        
        //        logdebug(@"%@",body);
        NSString *title = [NSString stringWithFormat:@"<h4>%@</h4>",self.newsTitle];
        //        NSLog(@"%@",self.newsTitle);
        
        body = [NSString stringWithFormat:@"%@%@%@",@"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>",title,mutStr];
        
        [webView loadHTMLString:body baseURL:nil];
        [hud hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //        logdebug(@"%@",error.localizedDescription);
        [hud hide: YES];
    }];
}
#pragma mark - web delegate
- (void)webViewDidStartLoad:(UIWebView *)webview{
    if (webview.request.URL.absoluteString.length < 1) {
        webview.scalesPageToFit = NO;
    }else{
        webview.scalesPageToFit = YES;
    }
    //    logdebug(@"%@",webView.request.URL.absoluteString);
}
- (void)webgoback{
    [self requestNewsContent];
}


@end
